public class SubsetSumIndividual extends Individual {
	private boolean[] genome;
	private int sum;
	private int[] values;
	
	public SubsetSumIndividual(int[] values, int sum) {
		this.genome = new boolean [values.length];
		this.sum = sum;
		this.values = values;
	}
	
	protected double evaluate() {
		int tSum = 0;
		for (int i = 0;i < genome.length;i = i+1) {
			if(genome[i]) {
				tSum = tSum+values[i];
			}
		}
		return Math.abs(tSum-sum);
	}
	
    public SubsetSumIndividual clone() {
    	SubsetSumIndividual ans;
    	ans = (SubsetSumIndividual)super.clone();
    	ans.genome = new boolean [genome.length];
    	for (int i = 0;i < genome.length;i = i+1) {
    		ans.genome[i] = this.genome[i];
    	}
    	return ans;
    }
    
    
	public Individual mutate() {
		SubsetSumIndividual other = this.clone();
		int indexToMutate = (int) (Math.random() * this.genome.length);
		other.genome[indexToMutate] = !(other.genome[indexToMutate]);
		return other;
	}

    public Individual crossover(Individual father) {
    	SubsetSumIndividual child = this.clone();
    	int rangeToChooseCrossoverIndex = (this.genome.length-2);
    	int indexToCrossoverFrom = 1 + (int) (Math.random() * rangeToChooseCrossoverIndex);
    	for (int i = indexToCrossoverFrom; i < this.genome.length ; i = i+1) {
    		child.genome[i] = ((SubsetSumIndividual) father).genome[i];
    	}
    	return child;
    }
    
    public String toString() {
    	String ans = "SubsetSumIndividual genome is:\n ";
    	for (int i = 0;i < this.genome.length;i = i+1) {
    		ans = ans + "index: " + i + " has " + this.genome[i] + "\n";
    	}
    	return ans;
    }
}
